<template>
  <div>
    <h1>系统推荐医师</h1>
    <div class="doctor-cards">
      <el-tooltip 
        v-for="doctor in doctorList" 
        :key="doctor.id"
        :content="doctor.remark" 
        placement="bottom"
      >
      <el-card 
        class="doctor-card"
        @click="openForm(doctor.id)"
      >
        <div class="doctor-avatar">
          <el-image 
            :src="doctor.avatar" 
            fit="cover"
            class="avatar-image"
          />
        </div>
        <div class="doctor-info">
          <h3>{{ doctor.nickname }}</h3>
          <p class="dept">{{ doctor.deptName }}</p>
          <p class="doctorType">
            {{ getIntDictOptions(DICT_TYPE.MEDICAL_DOCTOR_TYPE)
            .find(item => item.value === doctor.doctorType)?.label || doctor.doctorType }}
          </p>
          <p class="remark">{{ doctor.remark }}</p>
        </div>
      </el-card>

    </el-tooltip>
    </div>

    <DoctorScheduleForm ref="formRef"/>

  </div>
</template>
<script lang="ts" setup>
import { defineOptions } from 'vue'
import * as RatingApi from '@/api/medical/rating'
import * as DoctorApi from '@/api/medical/doctor'
import DoctorScheduleForm from './DoctorScheduleForm.vue'
import { getUserProfile } from '@/api/system/user/profile'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const message = useMessage() // 消息弹窗

const doctorList = ref([])// 医师列表

const patientId = ref()
const limit = 5
const doctorIds = ref([])// 存储推荐的医生ID

defineOptions({ name: 'DoctorRecommend' })

/** 添加/修改操作 */
const formRef = ref()
const openForm = (doctorId?: number) => {
  formRef.value.open(doctorId)
}

const getUserInfo = async () => {
  const users = await getUserProfile()
  patientId.value = users.id
  console.log(patientId.value)
}

const getRecommend = async () => {
  doctorIds.value = await RatingApi.getUserBasedRecommendations(patientId.value, limit);
  
  console.log(doctorIds)
  

  // 根据 doctorIds 来获取医生信息并展示  
  doctorList.value = await DoctorApi.getRecommenDoctorList(doctorIds.value)
}

/** 初始化 **/
onMounted(async () => {
  await getUserInfo()
  await getRecommend()
  
})
</script>

<style scoped>


/* 响应式调整 - 小屏幕时每行显示2个 */
@media (width <= 768px) {
  .doctor-cards {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* 响应式调整 - 超小屏幕时每行显示1个 */
@media (width <= 480px) {
  .doctor-cards {
    grid-template-columns: 1fr;
  }
}

.doctor-recommend {
  padding: 20px;
}

.doctor-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* 自动填充，最小宽度280px */
  gap: 16px;
  margin-top: 16px;
}

.doctor-card {
  display: flex;
  max-height: 350px;
  min-height: 300px;
  transition: all 0.3s ease; /* 添加过渡效果 */
  flex-direction: column;
}

.doctor-avatar {
  height: 120px; /* 缩小头像区域高度 */
  margin-bottom: 10px;
}

.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.doctor-info {
  padding: 0 10px;
  overflow-y: auto; /* 信息区域可滚动 */
  flex: 1;
}

.doctor-info p {
  margin: 5px 0;
  color: #666;
}

.dept {
  font-weight: bold;
  color: #409EFF !important;
}

.title {
  font-style: italic;
}
</style>